<?php
namespace Client\Controller;
use Think\Controller;
class OrderController extends BaseController{
//class OrderController extends Controller{
	
	//========================
	public static $ordertype;
	protected function _initialize(){
		parent::_initialize();
		header("Content-Type: text/html;charset=utf-8");
		$this->assign('tj',time());
		self::$ordertype=array('0'=>'派单中','1'=>'待收衣','2'=>'待支付','3'=>'待洗涤','4'=>'洗涤中','5'=>'待送衣','6'=>'已完成','7'=>'已评论','9'=>'已取消');
		$this->assign('ordertype',self::$ordertype);
		$this->assign('entry',self::$entry);
	}
	
	//首页
    public function index(){
		$this->display();
    }
	
	
	//list
	public function orderlist(){
		//=================
		$typeid=I('typeid') ? I('typeid')  : '1';
		if($typeid){
			$map['wash_type']=$typeid;
			$map['openid']=$_SESSION['openid'];
			$m=M('bb_order');
			$orderlist=$m->where($map)->order('addtime desc')->select();
			$this->assign('orderlist',$orderlist);
			$this->assign('typeid',$typeid);
			$this->display();
		}
    }
	//=========
	public function orderinfo(){
		$orderid=I('orderid');
		if($orderid){
			$m=M('bb_order');
			$order=$m->where(array('orderid'=>$orderid))->find();
			$this->assign('order',$order);
			$order2=M('bb_order_yifu')->where(array('orderid'=>$orderid))->select();
			$this->assign('yifulist',$order2);
			$order3=M('bb_order_img')->where(array('orderid'=>$orderid))->select();
			$this->assign('yifuimg',$order3);
			$this->display();
		}
	}
	
	//根据支付方试跳转到对应的支付接口
	public function pay(){
		$g=I('get.');
		$m=M('bb_order');
		$order=$m->where(array('orderid'=>$g['orderid']))->find();
		if($order['is_pay']!=0){
			echo "<script> alert('该订单已支付！'); history.go(-1);</script>";
		}
		if($g['paytype']=='wx'){
			$this->wxpay($order['total_price'],$order['orderid'],$_SESSION['user']['id']);
		}else if($g['paytype']=='zfb'){
			$this->zfbpay($order['total_price'],$order['orderid'],$_SESSION['user']['id']);
		}else{
			echo '你的支付占不支持';
		}
	}

	//微信支付代码
	public function wxpay($money,$orderId,$userid){
		header("Content-Type: text/html;charset=utf-8");
		
		//$money=$money;
		$money=0.01;
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$options['mch_id']=C('BB_MCHID_client');
		$options['payKey']=C('BB_MCHPAYKEY_client');
		$wx=new \bb\Wechat($options);
		
		$pay['openid']=$_SESSION['openid'];
		$pay['body']='预约洗衣';
		$pay['orderId']=time();
		$pay['money']=$money;
		$pay['attach']=$orderId.'|'.$userid;
		$pay['notify_url']='http://wx.tingzan88.com/Client/notify/wx_notify';
		$data=$wx->unifiedOrder($pay['openid'],$pay['body'],$pay['orderId'],$pay['money'],$pay['notify_url'],$pay['attach']);
		foreach($data as $key => $v) {
			$str .= "{$key}={$v}&";
		}
		echo $url="http://wx.tingzan88.com/wxpay.php?$str".'orderid='.$orderId."&money=".$pay['money'];
		header("Location:".$url); 
	}
	
	//支付宝支付代码
	public function zfbpay($money,$orderId,$userid){
		
		//$money=$money;
		$money=0.01;
		
		$zfb=new \bb\Alipaywap();
		$zfb->pay($money,$orderId,$userid);
	}
	
}